/*
 * @Author: Broin
 * @Date: 2021-12-29 09:43:06
 * @LastEditTime: 2022-01-04 22:32:11
 * @LastEditors: your name
 * @Description: 
 * @FilePath: /ac_interface/src/api/match.js
 * MADE BY BROIN
 */


import { users } from '../data/users';
import { virtualMachines } from '../data/vm';
import { findUser } from './users';
import { listAllTerminalbyType, findTerminalbyName } from './vm';


/**
 * @description 为当前用户关联一个虚拟机
 * @param {*} username 
 * @param {*} vmname 
 */
export function addvMatch(username, vmname){
    let index = findUser(username);
    for (let i = 0; i < users[index].machines.length; i++) {
        if(users[index].machines[i].name == vmname){
            console.log('已有这台虚拟机');
            alert('已有这台虚拟机，请勿重复添加');
            return {
                code: -1
            };
        }
    }
    users[index].machines.push({name:vmname});
    return {
        code: 0
    };
}

/**
 * @description 删除一个用户关联的虚拟机.
 * @param {*} username 
 * @param {*} vmname 
 * @returns 
 */
export function delMatch(username, vmname){
    let index = findUser(username);
    let uservmIndex;
    let hasVM = false;
    for (let i = 0; i < users[index].machines.length; i++) {
        if(users[index].machines[i].name == vmname){
            uservmIndex = i;
            hasVM = true;
        }
    }
    if(!hasVM){
        console.log('已删除虚拟机');
        alert('已删除虚拟机，无需再次删除');
        return {
            code: -1
        };
    }
    if(hasVM){
        let temp = users[index].machines[users[index].machines.length - 1];
        users[index].machines[users[index].machines.length - 1] =  users[index].machines[uservmIndex];
        users[index].machines[uservmIndex] = temp;
        users[index].machines.pop();
        return {
            code: 0
        };
    }
}

/**
 * @description 管理员界面：列出当前用户所能勾选的所有虚拟机
 * @param {*} username 
 */
export function canList(username){
    let index = findUser(username);
    let list = [];

    if (index == -1) {
        return {
            list: list,
            code: -1
        };
    }

    if(users[index].pRole == 'teacher'){
        list = listAllTerminalbyType('demo');
        list.push.apply(list, listAllTerminalbyType('teach'));
    }
    if(users[index].pRole == 'student'){
        list = listAllTerminalbyType('train');
        if(users[index].vmRole == 'normal'){
            list.push.apply(list, listAllTerminalbyType('teach'));
        }
    }
    return {
        list: list,
        code: 0
    };
}

/**
 * @description 列出当前用户可用虚拟机
 * @param {*} username 
 * @returns 
 */
export function listMatch(username){
    let index = findUser(username);
    let list = [];

    if (index == -1) {
        return {
            list: list,
            code: -1
        };
    }
    list = users[index].machines;
    return {
        list: list,
        code: 0
    };
}

export function showInfo(vmRole, vmname){
    let vmindex = findTerminalbyName(vmname);
    let host = virtualMachines[vmindex].IP;
    let name;
    let pass;
    if(vmRole == 'normal'){
        name = 'normal';
        pass = virtualMachines[vmindex].vmnormalpass;
    }
    if(vmRole == 'root'){
        name = 'root';
        pass = virtualMachines[vmindex].vmrootpass;
    }

    return {name, host, pass};
}